/*
 * This file is part of JGrasstools (http://www.jgrasstools.org)
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * JGrasstools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jgrasstools.hortonmachine.modules.statistics.cb;

import java.awt.image.RenderedImage;

import oms3.annotations.Author;
import oms3.annotations.Documentation;
import oms3.annotations.Label;
import oms3.annotations.Description;
import oms3.annotations.Execute;
import oms3.annotations.In;
import oms3.annotations.Keywords;
import oms3.annotations.License;
import oms3.annotations.Name;
import oms3.annotations.Out;
import oms3.annotations.Status;

import org.geotools.coverage.grid.GridCoverage2D;
import org.jgrasstools.gears.libs.modules.JGTConstants;
import org.jgrasstools.gears.libs.modules.JGTModel;
import org.jgrasstools.gears.libs.monitor.LogProgressMonitor;
import org.jgrasstools.gears.libs.monitor.IJGTProgressMonitor;
import org.jgrasstools.gears.utils.math.CoupledFieldsMoments;

@Description("Calculates the histogram of a set of data contained in a matrix "
        + "with respect to the set of data contained in another matrix.")
@Documentation("Cb.html")
@Author(name = "Andrea Antonello, Silvia Franceschi, Rigon Riccardo", contact = "http://www.hydrologis.com, http://www.ing.unitn.it/dica/hp/?user=rigon")
@Keywords("Histogram, Geomorphology, Statistic")
@Label(JGTConstants.STATISTICS)
@Name("cb")
@Status(Status.CERTIFIED)
@License("General Public License Version 3 (GPLv3)")
public class Cb extends JGTModel {
    @Description("The first raster to analyse.")
    @In
    public GridCoverage2D inRaster1 = null;

    @Description("The second raster to analyse.")
    @In
    public GridCoverage2D inRaster2 = null;

    @Description("The progress monitor.")
    @In
    public IJGTProgressMonitor pm = new LogProgressMonitor();

    @Description("The number of bins into which divide the data range.")
    @In
    public int pBins = 100;

    @Description("The first moment to calculate.")
    @In
    public int pFirst = 1;

    @Description("The last moment to calculate.")
    @In
    public int pLast = 2;

    @Description("A matrix containing " + "1) the mean value of the data in abscissa; "
            + "2) the number of elements in each interval; " + "3) the mean value of the data in ordinate; "
            + "n+2) the n-esimal moment of the data in ordinate.")
    @Out
    public double[][] outCb;

    private int binmode = 1;

    // private int bintype;
    // private float base;

    @Execute
    public void process() throws Exception {
        if (!concatOr(outCb == null, doReset)) {
            return;
        }

        RenderedImage map1RI = inRaster1.getRenderedImage();
        RenderedImage map2RI = null;
        if (inRaster2 == null) {
            map2RI = map1RI;
        } else {
            map2RI = inRaster2.getRenderedImage();
        }

        outCb = new CoupledFieldsMoments().process(map1RI, map2RI, pBins, pFirst, pLast, pm, binmode);

    }

}
